const BaseService = require('./BaseService.js');
const PageList = require('../model/PageList.js');
const AppConfig = require('../config/AppConfig');
const md5 = require('md5-node');

class AdminInfoService extends BaseService {
  constructor() {
    super();
    this.currentTableName = this.tableMap.admin_info;
  }
  /**
   * @description 分页查询
   * @param {{ admin_name, admin_tel, admin_email, admin_sex, pageIndex }} param 请求参数
   * @returns {Promise<PageList>} 返回分页查询以后的封装的PageList
   */
  async getAdminListByName({ admin_name, admin_tel, admin_email, admin_sex, pageIndex }) {
    let strSql = ` select id,admin_name,admin_sex,admin_tel,admin_email,admin_photo,admin_address from ${this.currentTableName} where isDel = 0 `;
    let countSql = ` select count(*) 'total_count' from ${this.currentTableName} where isDel = 0 `;
    let strWhere = `  `;
    let ps = [];
    if (admin_name) {
      strWhere += ` and admin_name like ? `;
      ps.push(`%${admin_name}%`);
    }
    if (admin_tel) {
      strWhere += ` and admin_tel like ? `;
      ps.push(`%${admin_tel}%`);
    }
    if (admin_email) {
      strWhere += ` and admin_name like ? `;
      ps.push(`%${admin_email}%`);
    }
    if (admin_sex) {
      strWhere += ` and admin_sex = ? `;
      ps.push(admin_sex);
    }
    strSql += ` ${strWhere} limit ${(pageIndex - 1) * this.pageSize}, ${this.pageSize} `;
    countSql += strWhere;
    let results = await this.executeSql(strSql + ';' + countSql, [...ps, ...ps]);
    let pageList = new PageList(pageIndex, results[1][0].total_count, this.pageSize, results[0]);
    return pageList;
  }
  /**
   * @description 新增一个管理员
   * @param {{ admin_name, admin_sex, admin_tel, admin_pwd, admin_email, admin_photo, admin_address }} param 新增的对象
   * @returns {Promise<boolean>}
   */
  async add({
    admin_name,
    admin_sex,
    admin_tel,
    admin_pwd,
    admin_email,
    admin_photo,
    admin_address
  }) {
    admin_pwd = md5(admin_pwd + AppConfig.md5Salt);
    let strSql = ` INSERT INTO ${this.currentTableName} ( admin_name, admin_sex, admin_tel, admin_pwd, admin_email, admin_photo, admin_address) VALUES (?, ?, ?, ?, ?, ?, ?); `;
    let results = await this.executeSql(strSql, [
      admin_name,
      admin_sex,
      admin_tel,
      admin_pwd,
      admin_email,
      admin_photo,
      admin_address
    ]);
    return results.affectedRows > 0 ? true : false;
  }

  /**
   * @description 检测登陆
   * @param {{zh,admin_pwd}} param 登陆的账号与密码
   * @returns {Promise<object>} 登陆查询的结果
   */
  async checkLogin({ zh, admin_pwd }) {
    let strSql = ` select * from ${this.currentTableName} where isDel = false and admin_pwd = ? `;
    admin_pwd = md5(admin_pwd + AppConfig.md5Salt);
    if (/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(zh)) {
      strSql += ` and admin_email = ? `;
    } else if (
      /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(zh)
    ) {
      strSql += ` and admin_tel = ? `;
    } else {
      strSql += ` and id = ? `;
    }
    let results = await this.executeSql(strSql, [admin_pwd, zh]);
    return results[0];
  }
}

module.exports = AdminInfoService;
